package com.tengju.settlement.infrastructure.repository;

import com.tengju.settlement.domain.model.CardBinRepository;
import com.tengju.settlement.infrastructure.persistence.mybatis.dataobject.CardBinDO;
import com.tengju.settlement.infrastructure.persistence.mybatis.mapper.tengju.custom.CardBinDOMapper;
import java.util.List;
import java.util.Optional;
import org.springframework.stereotype.Repository;

@Repository
public class CardBinRepositoryImpl implements CardBinRepository {

    private final CardBinDOMapper cardBinDOMapper;

    public CardBinRepositoryImpl(CardBinDOMapper cardBinDOMapper) {
        this.cardBinDOMapper = cardBinDOMapper;
    }

    @Override
    public String getBankName(String bankNo) {
        try {
            int cardBin = Integer.parseInt(bankNo.substring(0, 6));
            if (cardBin < 100000) {
                return "";
            }
            List<CardBinDO> result = cardBinDOMapper.selectByCardBin(String.valueOf(cardBin));
            return Optional.ofNullable(result).map(list -> list.get(0).getBankName()).orElse("");
        } catch (Exception e) {
            return "";
        }
    }

}
